﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UDS.Web.UI.Controls
{
    /// <summary>
    /// Represents a <b>LINK (rel=stylesheet)</b> html element in a web page.
    /// This element can be used only within the <b>HEAD</b> tag.
    /// </summary>
    [DefaultProperty("Href")]
    public class Style : WebControl
    {
        /// <summary>
        /// Gets or sets the destination URL or anchor point.
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Href
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the media type.
        /// The possible values are: screen, print and all.
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Media
        {
            get;
            set;
        }

        /// <summary>
        /// Renders the control.
        /// </summary>
        protected override void Render(HtmlTextWriter output)
        {
            if (string.IsNullOrEmpty(Href))
            {
                throw new InvalidOperationException("The property 'Href' is required!");
            }

            string h = ResolveClientUrl(Href);
            string m = Media;
            if (string.IsNullOrEmpty(m))
            {
                m = "all";
            }

            output.Write("<link rel=\"stylesheet\" href=\"");
            output.Write(h);
            output.Write("\" type=\"text/css\" media=\"");
            output.Write(m);
            output.Write("\" />");
        }
    }
}